#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPIEx.au3>
#include <Timers.au3>


#include <Debug.au3>
Local $dbg_title = "local window for debug"
_DebugSetup($dbg_title,False, 1)
_DebugOut("start")

Global $array, $array_dim[3]
Global $hImage, $hBitmap
Global Const $limit_l = 255/240/2
Global Const $limit_v = 255/240
Global Const $limit_r = 1
Global $x1 = 80, $x2 = 220

Global $if = "data3\FFA12B\info-10001.bmp"
Global $of = "info-10001-m.bmp"

Global $ary_digit[10]

_main()


Func main()
	load_data("digit")
	Local $search = FileFindFirstFile("data3\0x*")
	If $search = -1 Then
		_DebugOut("no dirs")
		Return
	EndIf
	Local $cnt = 100
	While True
		Local $dir = FileFindNextFile($search)
		If @error Then ExitLoop
		Local $fs =  FileFindFirstFile("data3\" & $dir & "\*.bmp")
		If $fs = -1 Then
			_DebugOut("no files in " & "data3\" & $dir & "\*.bmp")
			ContinueLoop
		EndIf
		While True
			$cnt += 1
			Local $file = FileFindNextFile($fs)
			If @error Then ExitLoop

			;If $cnt <> 246 Then
			;	ContinueLoop
			;EndIf

			Local $fn = "data3\" & $dir & "\" & $file
			Local $save = "tmp2\" & $cnt & "-" & $file
			_DebugOut("process: " & $fn & " for " & $save)
			make_array($fn)
			Local $d = parse1($dir,200)
			save_matrix($d,$save & ".txt")
			Local $r = search_data($d)
			If $r < 0 Then
				_DebugOut("****************** fail: " & $save & " res=" & $r)
			EndIf
			;convert_r(200)
			;store_array($save)
			;ExitLoop
		WEnd
		FileClose($fs)
		;ExitLoop
	WEnd
	FileClose($search)
EndFunc

Func save_matrix(ByRef $m,$fn)
	Local $fh = FileOpen($fn,BitOr(2,256))
	For $y = 0 To Ubound($m,1)-1
		Local $str = ""
		For $x = 0 To Ubound($m,2)-1
			$str &= $m[$y][$x]
		Next
		$str &= @CRLF
		FileWrite($fh,$str)
	Next
	FileClose($fh)
EndFunc


Func parse1($color,$limit)
	Local $sy
	;$color += 0
	Local $has = False
	For $y = 0 To $array_dim[1]
		If getPixel($x1,$y) = $color And getPixel($x2,$y) = $color Then
			;_DebugOut("ok: y=" & $y & " c=" & Hex($color,6) & " c1=" & Hex(getPixel($x1,$y),6) & " c2=" & Hex(getPixel($x2,$y),6))
			$has = True
		ElseIf $has Then
			;_DebugOut("FIND: y=" & $y & " c=" & Hex($color,6) & " c1=" & Hex(getPixel($x1,$y),6) & " c2=" & Hex(getPixel($x2,$y),6))
			$sy = $y
			ExitLoop
		EndIf
	Next
	Local $rb = Floor($array_dim[0]/2)
	Local $a = getFilledLine(10,$sy+1,$rb,$array_dim[1], $limit) ;find first line
	;_DebugOut("a[0] = " & $a[0] & " a[1] = " & $a[1])
	$sy = getEmptyLine($a[0],$a[1]+1,$rb,$array_dim[1], $limit)  ;find empty line
	;_DebugOut("sy = " & $y)
	$a = getFilledLine($a[0],$sy+1,$rb,$array_dim[1], $limit) ;find level line
	;_DebugOut("a[0] = " & $a[0] & " a[1] = " & $a[1])
	;Local $m = getMatrix($a[0], $a[1], $a[0] + 50, $a[1] + 15, $limit) ; find word
	Local $b = getFilledRow($a[0] + 50, $a[1], $array_dim[0], $a[1] + 15, $limit); Find digit
	;_DebugOut("b[0] = " & $b[0] & " b[1] = " & $b[1])
	Local $m = getMatrix($b[0], $a[1], $b[0] + 13, $a[1] + 15, $limit) ; ask digit
	Return $m
EndFunc

Func getMatrix($l,$t,$r,$b,$limit)
	;_DebugOut("getMatrix(" & $l & "," & $t & "," & $r & "," & $b & ")")
	Local $m[$b-$t+1][$r-$l+1]
	Local $last_y = $b + 1
	For $y = $t To $b
		Local $empty = True
		For $x = $l To $r
			If getPixelLight($x,$y) < $limit Then
				$m[$y-$t][$x-$l] = 1
				$empty = False
			Else
				$m[$y-$t][$x-$l] = 0
			EndIf
		Next
		If $empty Then
			If $last_y > $y Then
				$last_y = $y
			EndIf
		Else
			$last_y = $b + 1
		EndIf
	Next

	Local $last_x = $r + 1
	For $x = $l To $r
		Local $empty = True
		For $y = $t To $last_y
			If $m[$y-$t][$x-$l] Then
				$empty = False
			EndIf
		Next
		If $empty Then
			If $last_x > $x Then
				;_DebugOut("new last_x: " & $x)
				$last_x = $x
			EndIf
		Else
			$last_x = $r + 1
		EndIf
	Next
	;_DebugOut("last_x: " & $last_x & ", last_y: " & $last_y)

	Local $rm[$last_y-$t][$last_x-$l]
	For $x = $l To $last_x - 1
		For $y = $t To $last_y - 1
			$rm[$y-$t][$x-$l] = $m[$y-$t][$x-$l]
		Next
	Next
	Return $rm
EndFunc


Func getEmptyRow($l,$t,$r,$b,$limit)
	;_DebugOut("getEmptyRow(" & $l & "," & $t & "," & $r & "," & $b & ")")
	For $x = $l To $r
		Local $empty = True
		For $y = $t To $b
			If getPixelLight($x,$y) < $limit Then
				$empty = False
				ExitLoop
			EndIf
		Next
		If $empty Then
			Return $x
		EndIf
	Next
	Return $r
EndFunc

Func getFilledRow($l,$t,$r,$b,$limit)
	;_DebugOut("getFilledRow(" & $l & "," & $t & "," & $r & "," & $b & ")")
	For $x = $l To $r
		For $y = $t To $b
			If getPixelLight($x,$y) < $limit Then
				Local $a[2] = [$x,$y]
				Return $a
			EndIf
		Next
	Next
	Local $a[2] = [$r,$b]
	Return $a
EndFunc

Func getEmptyLine($l,$t,$r,$b,$limit)
	;_DebugOut("getEmptyLine(" & $l & "," & $t & "," & $r & "," & $b & ")")
	For $y = $t To $b
		Local $empty = True
		For $x = $l To $r
			If getPixelLight($x,$y) < $limit Then
				$empty = False
				ExitLoop
			EndIf
		Next
		If $empty Then
			Return $y
		EndIf
	Next
	Return $b
EndFunc

Func getFilledLine($l,$t,$r,$b,$limit)
	;_DebugOut("getFilledLine(" & $l & "," & $t & "," & $r & "," & $b & ")")
	For $y = $t To $b
		For $x = $l To $r
			If getPixelLight($x,$y) < $limit Then
				Local $a[2] = [$x,$y]
				Return $a
			EndIf
		Next
	Next
	Local $a[2] = [$l,$b]
	Return $a
EndFunc

Func getPixelLight($x,$y)
	Local $shift = $x + $y * $array_dim[0] + 1
	Return BitAND(BitShift(DllStructGetData($array, 1, $shift), 16), 0xFF)
EndFunc

Func getPixel($x,$y)
	Local $shift = $x + $y * $array_dim[0] + 1
	return BitAND(DllStructGetData($array, 1, $shift),0xffffff)
EndFunc

Func get_light_r($nColor)
	Return BitAND(BitShift($nColor, 16), 0xFF)
EndFunc

Func main_r()
	For $l = 100 To 180
		Local $file = "tmp\info-10002-" & $l & "-r.bmp"
		Local $t = TimerInit()
		make_array($if)
		Local $d1 = TimerDiff($t)
		convert_r($l)
		Local $d2 = TimerDiff($t)
		store_array($file)
		Local $d3 = TimerDiff($t)
		_DebugOut("r: " & $file & ": " & $d1 & " " & $d2 - $d1 & " " & $d3 - $d2)
	Next
EndFunc

Func _main()
	_GDIPlus_Startup()
	main()
	_GDIPlus_Shutdown()
EndFunc

Func convert_r($limit)
	Local $l = $limit * $limit_r
	For $i = 1 To $array_dim[2]
		Local $c = BitAND(DllStructGetData($array, 1, $i),0xffffff)
		Local $nv
		If get_light_r($c) < $l Then
			$nv = 0
		Else
			$nv = 0xffffff
		EndIf
		DllStructSetData($array, 1, $nv, $i)
	Next
EndFunc

Func make_array($fn)
	$hImage = _GDIPlus_ImageLoadFromFile($fn)
	If $hImage == -1 Then Return -1
	$hBitmap =_GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
	Local $h = _GDIPlus_ImageGetHeight($hImage)
	Local $w = _GDIPlus_ImageGetWidth($hImage)
	Local $length = $w * $h
	If $array Then
		_WinAPI_DeleteObject($array)
		_GDIPlus_ImageDispose ($hImage)
		_WinAPI_DeleteObject ($hBitmap)
	EndIf
	$array = DllStructCreate('dword[' & $length & ']')
	_WinAPI_GetBitmapBits($hBitmap, 4 * $length, DllStructGetPtr($array))
	$array_dim[0] = $w
	$array_dim[1] = $h
	$array_dim[2] = $length
EndFunc

Func store_array($fn)
	_WinAPI_SetBitmapBits($hBitmap, 4 * $array_dim[2], DllStructGetPtr($array))
	Local $hi = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
	_GDIPlus_ImageSaveToFile($hi,$fn)
	_GDIPlus_ImageDispose ($hi)
EndFunc


Func load_data($dir)
	Local $search = FileFindFirstFile($dir & "\*.txt")
	If $search = -1 Then
		_DebugOut("no files")
		Return
	EndIf
	While True
		Local $file = FileFindNextFile($search)
		If @error Then ExitLoop
		load_data_file($dir,$file)
	WEnd
	FileClose($search)
EndFunc

Func load_data_file($dir,$file)
	Local $fh = FileOpen($dir & "\" & $file)
	Local $ret2 = StringRegExp($file,"(\d+)",1)
	Local $number
	If @error = 0 Then
		$number = $ret2[0]
	Else
		_DebugOut("bad " & $file)
		Return
	EndIf
	If $number = 10 Then
		$number = 0
	EndIf
	Local $ary[50][50], $max_x = 0, $max_y = 0
	While True
		Local $line = FileReadLine($fh)
		If @error = -1 Then ExitLoop
		Local $ret = StringRegExp($line,"(\d)",3)
		If @error <> 0 Then
			_DebugOut("bad file: " & $file & " line: " & $line)
		EndIf
		$max_x = UBound($ret) - 1
		For $x = 0 To $max_x
			$ary[$max_y][$x] = $ret[$x]
		Next
		$max_y += 1
	WEnd
	$max_y -= 1
	FileClose($fh)
	Local $m[$max_y + 1][$max_x + 1]
	For $y = 0 To $max_y
		For $x = 0 To $max_x
			$m[$y][$x] = $ary[$y][$x]
		Next
	Next
	$ary_digit[$number] = $m
EndFunc

Func search_data(ByRef $m)
	Local $xm = UBound($m,2) - 1
	Local $ym = UBound($m,1) - 1
	For $i = 0 To 9
		Local $d = $ary_digit[$i]
		Local $my = UBound($d,1) - 1
		Local $has = True
		If $my <> $ym Then
			ContinueLoop
		EndIf
		For $y = 0 To $my
			Local $mx = UBound($d,2) - 1
			If $mx <> $xm Then
				$has = False
				ContinueLoop 2
			EndIf
			For $x = 0 To $mx
				If $m[$y][$x] <> $d[$y][$x] Then
					$has = False
					ExitLoop 2
				EndIf
			Next
		Next
		If $has Then
			Return $i
		EndIf
	Next
	Return -1
EndFunc
